package com.ruoyi.goods.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.goods.domain.BmCategory;
import com.ruoyi.goods.service.IBmCategoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 商品分类Controller
 * 
 * @author ruoyi
 * @date 2023-08-03
 */
@RestController
@RequestMapping("/category")
public class BmCategoryController extends BaseController
{
    @Autowired
    private IBmCategoryService bmCategoryService;

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("ruoyi-goods:category:list")
    @GetMapping("/list")
    public AjaxResult list(BmCategory bmCategory)
    {
        List<BmCategory> list = bmCategoryService.selectBmCategoryList(bmCategory);
        return AjaxResult.success(list);
    }

    /**
     * 商品一级分类
     * @return
     */

    @GetMapping("/firstList")
    public AjaxResult firstList()
    {
        List<BmCategory> list = bmCategoryService.selectBmCategoryFirstList();
        return AjaxResult.success(list);
    }

    /**
     * 商品二级分类
     * @return
     */
    @GetMapping("/secondList")
    public AjaxResult secondList()
    {
        List<BmCategory> list = bmCategoryService.selectBmCategorySecondList();
        return AjaxResult.success(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("ruoyi-goods:category:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmCategory bmCategory)
    {
        List<BmCategory> list = bmCategoryService.selectBmCategoryList(bmCategory);
        ExcelUtil<BmCategory> util = new ExcelUtil<BmCategory>(BmCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @RequiresPermissions("ruoyi-goods:category:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bmCategoryService.selectBmCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("ruoyi-goods:category:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmCategory bmCategory)
    {
        return toAjax(bmCategoryService.insertBmCategory(bmCategory));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("ruoyi-goods:category:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmCategory bmCategory)
    {
        return toAjax(bmCategoryService.updateBmCategory(bmCategory));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("ruoyi-goods:category:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bmCategoryService.deleteBmCategoryByIds(ids));
    }

    @GetMapping("/getTreeList")
    public AjaxResult getTreeList(){
        List<BmCategory> list = bmCategoryService.getTreeList();
        System.out.println("list="+list);
        return AjaxResult.success(list);
    }
}
